package com.stock.stockana.ui.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.stock.stockana.R;
import com.stock.stockana.StockCenter;
import com.stock.stockana.beans.ResponseBean;
import com.stock.stockana.beans.StockInfoBean;
import com.stock.stockana.constants.NetWorkConstants;
import com.stock.stockana.events.network.GetStockNewsEvent;
import com.stock.stockana.events.network.SetNotifiEvent;
import com.stock.stockana.requests.GetOneStock;
import com.stock.stockana.ui.activities.StockDetailActivity;
import com.stock.stockana.ui.adapter.StockDetailAdapter;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

/**
 * Created by ljgsonx on 2016/1/30.
 */
public class StockDetailFragment extends Fragment {

    private final static String TAG = "StockDetailFragment--->";

    private View mView;
    private RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    private StockDetailAdapter mStockDetailAdapter;

    private StockInfoBean sb;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            sb = (StockInfoBean) bundle.getSerializable("clickedStock");
        }
        mView = inflater.inflate(R.layout.recyclerview, container, false);
        mRecyclerView = (RecyclerView) mView.findViewById(com.stock.publiclib.R.id.id_recyclerview);
        mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mStockDetailAdapter = new StockDetailAdapter(getActivity(),sb);
        if (sb != null){
            ((StockDetailActivity) getActivity()).setToolbarPosTitle(sb.getStName());
            ((StockDetailActivity) getActivity()).setToolbarSubTitle("("+sb.getStNum()+")");
            mRecyclerView.setAdapter(mStockDetailAdapter);
            mRecyclerView.setLayoutManager(mLayoutManager);
            return mView;
        }
        return null;
    }

    @Subscribe
    public void onEventMainThread(SetNotifiEvent event) {
        ResponseBean rb = event.getmResponseBean();
        if (rb.getRespCode() == NetWorkConstants.RESPONSE_CODE_OK) {
            Toast.makeText(getActivity(), "设置成功", Toast.LENGTH_LONG).show();
            GetOneStock gos = GetOneStock.build().setStockNum(sb.getStNum()).setAction(GetOneStock.ACTION_UPDATEPRICE);
            StockCenter.doReq(gos);
            mStockDetailAdapter.setSureSettingBtnText("重新设置");
            mStockDetailAdapter.updateStockPrice();
        }else {
            Log.d(TAG, rb.getErrorMsg());
            Toast.makeText(getActivity(), "设置失败", Toast.LENGTH_LONG).show();
        }
    }

    @Subscribe
    public void onEventMainThread(GetStockNewsEvent event) {
        if (event.getResponseCode() == NetWorkConstants.RESPONSE_CODE_OK) {

            mStockDetailAdapter.updateStockNewsList();

        }else {
            Log.d(TAG, event.getMessage());
            Toast.makeText(getActivity(), "获取股票新闻失败,请重试!", Toast.LENGTH_LONG).show();
        }
    }

    public void enableSureSettingBtn(boolean enable){
        if(enable) mStockDetailAdapter.enableSureSettingBtn();
        else mStockDetailAdapter.disableSureSettingBtn();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
